package Thread;


import java.util.Collection;
import java.util.Comparator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

class MyThreadPool{
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();

    public MyThreadPool(int p) {
        for (int i = 0;i < p;i++){
            Thread t = new Thread(()->{
                try {
                    while (true){
                        Runnable runnable = queue.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        t.start();
        }
    }
    public void submit(Runnable runnable) throws InterruptedException{
        queue.put(runnable);
    }

}

public class ThreadDemo21 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int tmp = i;
                myThreadPool.submit(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("hello" + tmp);
                    }
                });
        }
    }
}
